package com.tech.view.common;

import com.tech.util.LogProcessUtil;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.function.Predicate;
import java.util.logging.Logger;

import static com.tech.util.FileUtil.deleteFileDir;

/**
 * 资源加载工具类
 */
public class ResourcesUtil {
	private final static Logger LOG = LogProcessUtil.getLogger(ResourcesUtil.class.getName());
	// 公共目录
	private static String DIRECTORY = "/";

	static {
		// 创建一些目录
		URL url = ResourcesUtil.class.getResource(DIRECTORY);
		Arrays.stream(CacheDir.values()).forEach(cacheDir -> {
			File file = new File(url.getPath() + cacheDir.getDir());
			if (!file.isDirectory()) file.mkdirs();
		});

	}

	public static void main(String[] args) {
		System.out.println(System.currentTimeMillis());
	}

	/**
	 * 通过枚举类型加载指定的图像资源，不存在时返回 null
	 *
	 * @param image 要加载哪一个图像
	 * @return 图像，找不到的话返回null
	 */
	public static BufferedImage getImage(Image image) {
		return getImageByPath(DIRECTORY + image.filePath);
	}

	/**
	 * 通过枚举类型加载指定的图像资源，不存在时返回 null
	 *
	 * @param path 相对路径
	 * @return 图像，找不到的话返回null
	 */
	public static BufferedImage getImageByPath(String path) {
		BufferedImage result = null;
		InputStream inputStream = ResourcesUtil.class.getResourceAsStream(path);
		if (inputStream == null) return null;
		try {
			result = ImageIO.read(inputStream);
		} catch (IOException e) {
			// 图片加载失败
			e.addSuppressed(new RuntimeException("无法读取图片：" + path));
			LOG.throwing(ResourcesUtil.class.getName(), "getImage(IMAGE imageName)", e);
		}

		return result;
	}

	public static String getAbsolutePathByPath(String path) {
		return ResourcesUtil.class.getResource(DIRECTORY).getFile() + path;
	}

	/**
	 * 删除给定目录缓存
	 *
	 * @param dir 给定目录，填null就删除所有缓存
	 */
	public static void clearCache(CacheDir dir) {
		try {
			if (dir != null) {
				deleteFileDir(getAbsolutePathByPath(dir.getDir()));
			} else {
				for (CacheDir d : CacheDir.values()) {
					deleteFileDir(getAbsolutePathByPath(d.getDir()));
				}
			}
		} catch (IOException e) {
			LOG.throwing(ResourcesUtil.class.getName(), "clearCache", e);
			throw new RuntimeException(e);
		}
	}

	public enum Image {
		LOGO("logo", "images/logo2.png"),
		BOTTOM_ARROW_ICON("向下箭头图标", "images/bottom_arrow.png"),
		KEYBOARD_ICON("键盘图标", "images/keyboard.png"),
		PASSWORD_ICON("密码图标", "images/password.png"),
		USER_ICON("用户图标", "images/user.png"),
		EMAIL_ICON("用户邮箱", "images/email.png"),
		// PORT_BACKGROUND("公告背景图","images/port_background.png"),
		PORT_BACKGROUND("公告背景图", "images/port_mock.png"),
		CLOSE_ICON("窗口关闭图标", "images/close_icon.png"),
		MAX_ICON("窗口最大化图标", "images/max_icon.png"),
		MIN_ICON("窗口最小化图标", "images/min_icon.png"),
		MENU_ICON("菜单图标", "images/menu_icon.png"),
		CAR_ICON("购物车图标", "images/car_icon.png"),
		MESSAGE_ICON("短消息图标", "images/message_icon.png"),
		SEND_GOODS_ICON("商品发布图标", "images/send_goods.png"),

		CHOICENESS("精选图标", "images/choiceness.png"),
		HOT("热门图标", "images/hot.png"),
		ALL_GOODS("所有图书图标", "images/all_goods.png"),

		CANCEL("返回", "images/cancel.png"),
		SAVE_DRAFT("存草稿", "images/save_draft.png"),
		SEND_GOODS("发布", "images/send.png"),
		SEND_GOODS_IMAGE("上传商品照片", "images/upload_image.png"),

		ADD_DIG("加一", "images/add.png"),
		SUB_DIG("减一", "images/sub.png"),
		EDIT_GOODS("编辑", "images/edit_button.png"),
		DEL_GOODS("删除", "images/del_button.png"),
		PURCHASE_GOODS("我想要", "images/purchase_button.png"),
		SEARCH_ICON("搜索", "images/search_icon.png"),
		DEFAULT_GOODS_IMAGE("默认商品图片", "images/default_goods_image.png");

		/**
		 * 枚举类型的说明，可以视为html中image标签的alt属性，补充说明的作用
		 */
		private final String state;
		/**
		 * 在 DIRECTORY 目录下文件的相对路径
		 */
		private String filePath;

		Image(String state, String fileName) {
			this.state = state;
			this.filePath = fileName;
		}

		public String getState() {
			return state;
		}

		public String getFilePath() {
			return filePath;
		}

		@Override
		public String toString() {
			return state;
		}
	}

	public enum CacheDir {
		GOODS_CARD("cache/goods_card/"), //用户访问的商品图片缓存目录，存储缩放后图片
		GOODS_DETAIL("cache/goods_detail/") //用户访问的商品图片缓存目录，存储缩放后图片
		;

		private final String dir;

		CacheDir(String dir) {
			this.dir = dir;
		}

		public String getDir() {
			return dir;
		}
	}
}
